var class_num=location.search;
class_num=class_num.split('?')[1];

if (islogin()) {


    $.post({
        url: "../../requests/teacher/select_class_name.php",
        data: {'class_num': class_num},
        async: false,
        success: function (data) {
            if (data != 'none') {
                $('.class_name').text(data);
            }
        }
    });
    $('#btn_add_sub').click(function () {
        var dom = "<div class='list-block'>" +
            "<ul>" +
            "<li class='item-content'>" +
            "<div class='item-inner'>" +
            "<div class='item-title label'>小节序号</div>" +
            "<div class='item-input'>" +
            "<input type='number' name='sub_num_add'>" +
            "</div>" +
            "</div>" +
            "</li>" +
            "<li class='item-content'>" +
            "<div class='item-inner'>" +
            "<div class='item-title label'>小节名</div>" +
            "<div class='item-input'>" +
            "<input type='text' name='sub_name_add'>" +
            "</div>" +
            "</div>" +
            "</li>" +
            "</ul>" +
            "</div>";
        $('.box').append(dom);
    });
    $('#btn_add').click(function () {
        var sec_num=$("input[name='sec_num']").val();
        var sec_name=$("input[name='sec_name']").val();
        var add_sub_names=new Array();
        var add_sub_sums=new Array();
        $("input[name='sub_num_add']").each(function () {
            add_sub_sums.push($(this).val());
        });
        $("input[name='sub_name_add']").each(function () {
            add_sub_names.push($(this).val());
        });
        if (sec_num != "" && sec_name != "") {
            $.post({
                url:'../../requests/teacher/add_course.php',
                data:{
                    'class':class_num,
                    'sec_num':sec_num,
                    'sec_name':sec_name,
                    'sub_num':add_sub_sums,
                    'sub_name':add_sub_names
                },
                async:false,
                success:function (data) {
                    if (data.indexOf('sub_exist')> 0) {
                        $$.alert('添加成功,已跳过重复小节', function () {
                            window.location.href = 'course_detail.html?' + class_num;
                        });
                    }
                    if (data == 'sec_exist') {
                        $$.alert('添加失败，章节号已存在', function () {
                            window.location.href = 'edit_course.html?' + class_num;
                        });
                    }
                    else {
                        $$.alert('添加成功', function () {
                            window.location.href = 'details_course.html?' + class_num;
                        });
                    }
                }
            });
        }
        else $$.alert('章节序号和章节名不能为空');
    });


}
else window.location.href='../../index.html';